package com.mall.common.util;

import java.util.Random;

import com.jfinal.log.Log;

public class NumberUtils {
	
	private static final Log logger = Log.getLog(NumberUtils.class);
	
	/**
	 * 生成随机短信验证码
	 * @param length
	 * @return
	 */
	public static String getNumberCode(int length){
		StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for(int i = 0; i < length; i++){
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num)+""), "");
        }
        return sb.toString();
	}
	
	/**
	 * 生成单号
	 * 当前时间+五位数字的随机数
	 * @return
	 */
	public static String getOrderCode(){
		return DateUtil.getDateCode().replaceAll("-","") + getNumberCode(5);
	}
	
	/**
     * 随机生成指定length的16进制数
     * @param length
     * @return
     */
    public static String randomHexString(int length){
        try {
            StringBuffer result = new StringBuffer();
            for(int i = 0; i < length; i++) {
                result.append(Integer.toHexString(new Random().nextInt(16)));
            }
            return result.toString().toUpperCase();
        } catch (Exception e) {
            logger.error("生成随机数异常", e);
        }
        return null;
    }
}
